package com.mxpio.erp.oa.controller;

import com.mxpio.erp.common.oa.entity.ReimburseInvoiceLine;
import com.mxpio.erp.common.oa.service.ReimburseInvoiceLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ReimburseInvoiceLineController", description = "财务报销-发票-明细行")
@RestController
@RequestMapping("/erp/oa/reimburse/invoice/line")
public class ReimburseInvoiceLineController {
    @Autowired
    private ReimburseInvoiceLineService reimburseInvoiceLineService;

    @GetMapping("list")
    @Operation(summary = "发票单列表-明细", description = "发票单列表-明细（无分页）", method = "GET")
    public Result<List<ReimburseInvoiceLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ReimburseInvoiceLine> list = reimburseInvoiceLineService.list(ReimburseInvoiceLine.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "发票单列表-明细", description = "发票单列表-明细（分页）", method = "GET")
    public Result<Page<ReimburseInvoiceLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ReimburseInvoiceLine> pageResult = reimburseInvoiceLineService.listPage(ReimburseInvoiceLine.class, page, criteria);
        return Result.OK(pageResult);
    }

//    @RequestMapping("add")
//    @Operation(summary = "保存发票单列表-明细", description = "保存发票单列表-明细")
//    public Result<ReimburseInvoiceLine> add(@RequestBody ReimburseInvoiceLine reimburseInvoiceLine) {
//        reimburseInvoiceLineService.save(reimburseInvoiceLine);
//        return Result.OK(reimburseInvoiceLine);
//    }

//    @PutMapping("edit")
//    @Operation(summary = "编辑", description = "编辑", method = "PUT")
//    public Result<ReimburseInvoiceLine> edit(@RequestBody ReimburseInvoiceLine reimburseInvoiceLine) {
////        ReimburseInvoiceLine byId = reimburseInvoiceLineService.getById(ReimburseInvoiceLine.class,reimburseInvoiceLine.getBizNo());
////        if (!byId.getOrderStatus().equals(OaEnums.OrderStatus.NEW.getCode())){
////            return Result.error("在新增状态下才可修改");
////        }
//        reimburseInvoiceLineService.update(reimburseInvoiceLine);
//        return Result.OK(reimburseInvoiceLine);
//    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除单子", description = "删除单子,多个id用英文逗号分隔", method = "DELETE")
    public Result<ReimburseInvoiceLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            reimburseInvoiceLineService.delete(ReimburseInvoiceLine.class, key);
        }
        return Result.OK();
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "通过发票单编码查询明细行", description = "通过发票单编码查询明细行（无分页）", method = "GET")
    public Result<List<ReimburseInvoiceLine>> listReim(@PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria criteria = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<ReimburseInvoiceLine> list = reimburseInvoiceLineService.list(ReimburseInvoiceLine.class,criteria);
        return Result.OK(list);
    }

}
